<?php
include('rootlogin.php');
//include('../../incsession.php');
session_start();
$level=2;
if($_POST['submit']=="submit"){
	$i=1;
	$number=$_POST['MC'];
	$num=$_POST['num'];
	while($i < $num)
	{	
		$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
		$Q = str_replace('"', '&quot;', $Q);
		$A = str_replace("'", "&#39;", $_POST['A'.$i]);
		$A = str_replace('"', '&quot;', $A);
		$B = str_replace("'", "&#39;", $_POST['B'.$i]);
		$B = str_replace('"', '&quot;', $B);
		$C = str_replace("'", "&#39;", $_POST['C'.$i]);
		$C = str_replace('"', '&quot;', $C);
		$choice=$_POST['answer'.$i];
		$ans=$_POST[$choice.$i];
		$ans = str_replace("'", "&#39;", $ans);
		$ans = str_replace('"', '&quot;', $ans);
		if($_POST['Q'.$i]=='' && $_POST['A'.$i]=='' && $_POST['B'.$i]=='' && $_POST['C'.$i]==''){
			
			
			$query = "DELETE FROM multiple_choice  
							WHERE mcid=".$_POST['mcid'.$i]." AND topicid=".$_SESSION['topic']." AND level=".$level;
			$result = mysql_query($query,$user_con);
		}
		if($_POST['Q'.$i]!='' && $_POST['A'.$i]!='' && $_POST['B'.$i]!='' && $_POST['C'.$i]!='' && $_POST['answer'.$i]!='' && 
			$_POST['Q'.$i]!=' ' && $_POST['A'.$i]!=' ' && $_POST['B'.$i]!=' ' && $_POST['C'.$i]!=' ' && $_POST['answer'.$i]!=' '){
			
			
			$query = "UPDATE multiple_choice SET question='".$Q."',
							a='".$A."', 
							b='".$B."',
							c='".$C."',
							answer='".$ans."' 
							WHERE mcid=".$_POST['mcid'.$i]." AND topicid=".$_SESSION['topic']." AND level=".$level;
			$result = mysql_query($query,$user_con);
		}
		$i++;
	}
	while($i<$number+$num)
	{
		$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
		$Q = str_replace('"', '&quot;', $Q);
		$A = str_replace("'", "&#39;", $_POST['A'.$i]);
		$A = str_replace('"', '&quot;', $A);
		$B = str_replace("'", "&#39;", $_POST['B'.$i]);
		$B = str_replace('"', '&quot;', $B);
		$C = str_replace("'", "&#39;", $_POST['C'.$i]);
		$C = str_replace('"', '&quot;', $C);
		$choice=$_POST['answer'.$i];
		$ans=$_POST[$choice.$i];
		$ans = str_replace("'", "&#39;", $ans);
		$ans = str_replace('"', '&quot;', $ans);
		if($_POST['Q'.$i]!='' && $_POST['A'.$i]!='' && $_POST['B'.$i]!='' && $_POST['C'.$i]!='' && $_POST['answer'.$i]!=''){
			
			
			$query = "INSERT INTO multiple_choice (question, a, b, c, answer, topicid, level) 
					VALUES ('".$Q."',
							'".$A."', 
							'".$B."',
							'".$C."',
							'".$ans."',
							".$_SESSION['topic'].", ".$level.")";
			$result = mysql_query($query,$user_con);
		}
		$i++;
	}
	header("location: page.php");	
}
if(!isset($_POST['MC'])){
			$MC=0;
			$flag=1;
}
else if($_POST['submit']=='add'){
			$MC=$_POST['MC'];
			$MC++;
			$flag=0;
}


?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../framestyle.css" type="text/css" media="screen">
<style type="text/css">

</style>
</head>

<body>
<div id="wrapper">
<div id="title">Online Listening Comprehension Program</div>

<div id="main">
<hr/>
<div id="title2">FOR INTERMEDIATE LEVEL: MULTIPLE CHOICE</div>
<hr/>
<br><br>

<form method = POST action="multiple_choice2.php" >
<?php
$i=1;



$query = "SELECT * FROM multiple_choice WHERE topicid=".$_SESSION['topic']." AND level=".$level;
$result = mysql_query($query,$user_con);
while($row = mysql_fetch_array($result)){
	if($flag==1){
		if($row['answer']==$row['a'])
			$ans="A";
		else if($row['answer']==$row['b'])
			$ans="B";
		else if($row['answer']==$row['c'])
			$ans="C";
		$Q=$row['question'];
		$A=$row['a'];
		$B=$row['b'];
		$C=$row['c'];
	}else{
		$ans=$_POST['answer'.$i];	
		$Q=$_POST['Q'.$i];
		$A=$_POST['A'.$i];
		$B=$_POST['B'.$i];
		$C=$_POST['C'.$i];
	}
	$Q = str_replace("'", "&#39;", $Q);
	$Q = str_replace('"', '&quot;', $Q);
	$A = str_replace("'", "&#39;", $A);
	$A = str_replace('"', '&quot;', $A);
	$B = str_replace("'", "&#39;", $B);
	$B = str_replace('"', '&quot;', $B);
	$C = str_replace("'", "&#39;", $C);
	$C = str_replace('"', '&quot;', $C);
	echo '<table>';
	echo '<tr><td align="right">Q'.$i.': <td><input class="area long" type="text" name="Q'.$i.'" value="'.$Q.'">
			<tr><td align="right">A: <td><input class="area long" type="text" name="A'.$i.'" value="'.$A.'">
			<tr><td align="right">B: <td><input class="area long" type="text" name="B'.$i.'" value="'.$B.'">
			<tr><td align="right">C: <td><input class="area long" type="text" name="C'.$i.'" value="'.$C.'">
			<tr><td align="right"><td>answer: <select name="answer'.$i.'">
			<option value='.$ans.'>'.$ans.'</option>
			<option value="A">A</option>
			<option value="B">B</option>
			<option value="C">C</option>
			</select></table><br>';	
	echo "<input type='hidden' name='mcid".$i."' value=".$row['mcid'].">";
	$i++;	
}
$num=$i;

while($i<$MC+$num){
	$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
	$Q = str_replace('"', '&quot;', $Q);
	$A = str_replace("'", "&#39;", $_POST['A'.$i]);
	$A = str_replace('"', '&quot;', $A);
	$B = str_replace("'", "&#39;", $_POST['B'.$i]);
	$B = str_replace('"', '&quot;', $B);
	$C = str_replace("'", "&#39;", $_POST['C'.$i]);
	$C = str_replace('"', '&quot;', $C);

	echo '<table>';
	echo '<tr><td align="right">Q'.$i.': <td><input class="area long" type="text" name="Q'.$i.'" value="'.$Q.'">
			<tr><td align="right">A: <td><input class="area long" type="text" name="A'.$i.'" value="'.$A.'">
			<tr><td align="right">B: <td><input class="area long" type="text" name="B'.$i.'" value="'.$B.'">
			<tr><td align="right">C: <td><input class="area long" type="text" name="C'.$i.'" value="'.$C.'">
			<tr><td align="right"><td>answer: <select name="answer'.$i.'">
			<option value='.$_POST['answer'.$i].'>'.$_POST['answer'.$i].'</option>
			<option value="A">A</option>
			<option value="B">B</option>
			<option value="C">C</option>
			</select></table>';	
	echo "<br>";
	$i++;	
}


echo "<input type='hidden' name='num' value=".$num.">";
echo "<input type='hidden' name='MC' value=".$MC.">";

?>
<div class="btn_right"><input type="submit" name = "submit" value = "add"></div><br>

<div class="btn_right"><input type="submit" name = "submit" value = "submit"></div>
</form>



</div>
</div>
</body>

